﻿using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Card.Make.Pro.Application.Helper;

namespace Card.Make.Pro.Form
{
    public partial class FrmAttendeeQuery : DevExpress.XtraEditors.XtraForm
    {
        public FrmAttendeeQuery()
        {
            InitializeComponent();
            this.txtKeyword.Edit.KeyDown += new KeyEventHandler(Edit_KeyDown);
        }


        void Edit_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                LoadingHelper.Open();

                var value = (sender as DevExpress.XtraEditors.TextEdit)?.EditValue;
                txtKeyword.EditValue = value;
                Query();
                LoadingHelper.Close();
            }
        }

        SqlHelper sqlHelper = new SqlHelper();
        private void butQuery_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LoadingHelper.Open();
            Query();
            LoadingHelper.Close();
        }

        private void Query()
        {
            string CommandText;
            var userInput = this.txtKeyword.EditValue?.ToString();
            if (string.IsNullOrWhiteSpace(userInput))
            {
                CommandText = "select top 100 UserName,C_ID,Photo,UserCertifyCardNo,CardUnit,CardDepart,CardType from Card_Table";
            }
            else
            {
                CommandText = "select UserName,C_ID,Photo,UserCertifyCardNo,CardUnit,CardDepart,CardType from Card_Table where UserName like '%" + userInput + "%'"
                              + " or  CardGroup like '%" + userInput + "%'"
                              + " or  CardNO like '%" + userInput + "%'"
                              + " or  CardUnit like '%" + userInput + "%'"
                              + " or  UserCertifyCardNo like '%" + userInput + "%'"
                              + " or  C_ID like '%" + userInput + "%'"
                              + " or  CardType like '%" + userInput + "%'";

            }
            DataTable dt = sqlHelper.GetQueryResult(CommandText);
            this.gridControl1.DataSource = dt;
        }



        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            System.Data.DataRowView pView = gridView1.GetFocusedRow() as System.Data.DataRowView; //聚焦行
            if (pView != null)
            {
                Frm_Detail frm = new Frm_Detail();
                frm.Id = pView.Row.ItemArray[1].ToString();
                frm.ShowDialog();
            }
        }

        private void FrmAttendeeQuery_KeyPress(object sender, KeyPressEventArgs e)
        {

        }
    }
}